#!/bin/tcsh -fe
####
#
#############################################################################
#                                                                           #
# Title: Import Movie from SerialEM                                         #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/21/2011                                             #
# Last Modification: 03/21/2011                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 10
#
# MANUAL: <B>Welcome to the File Importer</B>
#
# DISPLAY: imagename_original
# DISPLAY: imagenumber
# DISPLAY: tempkeep
# DISPLAY: comment
# DISPLAY: crop_histogram
# DISPLAY: crop_histogram_stdev
# DISPLAY: import_rawstack
# DISPLAY: import_rawstack_original
# DISPLAY: import_rawstack_type
# DISPLAY: import_gainref
# DISPLAY: import_gainref_original
# DISPLAY: import_defects
# DISPLAY: import_defects_original
# DISPLAY: raw_gaincorrectedstack
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: Calc_from_sample_pixel
# DISPLAY: sample_pixel
# DISPLAY: take_header_orig_sample_pixel
# DISPLAY: orig_sample_pixel
# DISPLAY: movie_imagenumber_total
# DISPLAY: import_bin
# DISPLAY: import_bin_target
# DISPLAY: SERIALEM_FACTOR
# DISPLAY: SERIALEM_DEFAULT_FACTOR
# DISPLAY: movie_image_dose_source
# DISPLAY: movie_image_dose_factor
# DISPLAY: movie_image_dose_manually
# DISPLAY: frame_measured_image_dose
# DISPLAY: frame_image_dose
# DISPLAY: frame_dose
# DISPLAY: total_dose
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: do_unpack
# DISPLAY: Thread_Number
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
# Variables to be available from the config file are:
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set crop_histogram = ""
set crop_histogram_stdev = ""
set movie_stackname = ""
set import_rawstack = ""
set import_rawstack_type = ""
set import_gainref = ""
set import_gainref_original = ""
set import_defects = ""
set import_defects_original = ""
set raw_gaincorrectedstack = ""
set magnification = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set take_header_orig_sample_pixel = ""
set orig_sample_pixel = ""
set movie_imagenumber_total = ""
set import_bin = ""
set import_bin_target = ""
set import_rawstack_original = ""
set SERIALEM_FACTOR = ""
set SERIALEM_DEFAULT_FACTOR = ""
set import_original_time = ""
set frame_image_dose_source = ""
set frame_image_dose_factor = ""
set frame_image_dose_manually = ""
set movie_image_dose_source = ""
set movie_image_dose_factor = ""
set movie_image_dose_manually = ""
set frame_measured_image_dose = ""
set CS = ""
set KV = ""
set defocus_phase_shift_doit = ""
set Thread_Number = ""
set export_basedir = ""
set export_rawstack_subdir = ""
set do_unpack = ""
#
#$end_vars
#
#
#
#
#
echo ${import_rawstack_original}

set scriptname = import_serialem
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
set import_produce_gainref2D = "y"
set import_produce_gainref2Dfft = "y"
#
setenv OMP_NUM_THREADS ${Thread_Number}
#
if ( ${website_blur_doit} == "y" ) then
  set blurkernel = ${website_blurkernel}
else
  set blurkernel = 0
endif
#
set namestub = `echo ${import_rawstack} | sed -e 's/\.mrc//g' -e 's/\.tif//g' | sed -e 's/\./_/g' -e 's/\,/_/g' -e 's/ /_/g' -e's/:/_/g' -e's/#/_/g'`
if ( ${import_rawstack_type} == "1" ) then
  set raw_gaincorrectedstack = ${namestub}_raw
endif
#
echo "<<@progress: 10>>"
#
if ( ${CS} == "ScriptWillPutNumberHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumberHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif

#
if ( ${defocus_phase_shift_doit} == "-" ) then
  set defocus_phase_shift_doit = ${Default_phase_shift_doit}
  echo "set defocus_phase_shift_doit = ${defocus_phase_shift_doit}" >> LOGS/${scriptname}.results
endif
#
if( ${import_original_time} == "-" || ${import_original_time} == "" ) then
  @ status_date = `date +%s` * 1000
  set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | ${app_gawk} '{print strftime("%c", $0)}' `
  # set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
  echo "set import_original_time = ${status_date}" >> LOGS/${scriptname}.results
else
  set status_date = ${import_original_time}
  set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | ${app_gawk} '{print strftime("%c", $0)}' `
  # set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
endif
#
if ( ! -e ${import_rawstack} ) then
  if ( -e ${import_rawstack_original} ) then
    rsync -auvP ${import_rawstack_original}  ${import_rawstack}
  endif
endif
#
if ( ${import_rawstack_type} == "1" ) then
  if ( ! -e ${import_rawstack} ) then
    set import_rawstack_exportname = ${export_basedir}/${export_rawstack_subdir}/${import_rawstack}
    ${proc_2dx}/linblock "ERROR: Raw stack ${import_rawstack} not found. Trying to find it in ${import_rawstack_exportname}"
    if ( -e ${import_rawstack_exportname} ) then
      rsync -auvP ${import_rawstack_exportname} ${import_rawstack}
      ${proc_2dx}/linblock "Raw stack found and copied back."
    else
      ${proc_2dx}/protest "ERROR: Raw stack ${import_rawstack} not found."
    endif
  endif    
  if ( "${import_gainref}" != "" ) then
    if ( ! -e "${import_gainref}" ) then
      if ( "${import_gainref_original}"x != "x" ) then
        if ( ! -e "${import_gainref_original}" ) then
          ${proc_2dx}/protest "ERROR: Gain reference "${import_gainref}" not found."
        else
          ${proc_2dx}/linblock "WARNING: Gain reference missing. Copying it again here."
          \cp -f "${import_gainref_original}" ..
          # set import_gainref = `echo "${import_gainref_original}" | rev | cut -d\/ -f1 | rev `
          # echo "set import_gainref = "${import_gainref}"" >> LOGS/${scriptname}.results
        endif
      else
        ${proc_2dx}/protest "WARNING: Gain reference missing."
      endif
    else
      ${proc_2dx}/linblock "WARNING: No gain reference specified."
    endif
    echo "# IMAGE-IMPORTANT: "${import_gainref}" <Gain reference (2D)>" >> LOGS/${scriptname}.results
    echo "::Gain reference found."
  endif
  echo "# IMAGE-IMPORTANT: ${import_rawstack} <Raw, dark-sub. stack (stack)>" >> LOGS/${scriptname}.results
  echo "::Raw movie as dark-subtracted stack present."
  echo "<<@evaluate>>"
  #

  set import_rawstack_end = `echo ${import_rawstack} | rev | cut -c1-4 | rev`
  if ( ${import_rawstack_end} == "mrcs" ) then
    set SERIALEM_FACTOR = 1.0
  else if ( ${import_rawstack_end} == ".mrc" ) then
    set SERIALEM_FACTOR = 1.0
  else if ( ${import_rawstack_end} == ".tif" ) then
    set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
  else if ( ${import_rawstack_end} == "tiff" ) then
    set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
  else
    ${proc_2dx}/protest "ERROR: file format ${import_rawstack_end} not supported."
  endif
  # 

  if ( ${do_unpack} == "y" ) then
    time nice ${dir_imod}/bin/clip info ${import_rawstack}
    # set float = " "
    set float = "-m 2"
    if ( "${import_defects}"x != "x" ) then
      if ( "${import_defects}"x != "../.x" ) then
        if ( -e "${import_defects}" ) then
          #################################################################################
          ${proc_2dx}/linblock "Running clip unpack ${float} -D "${import_defects}" ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
          #################################################################################
          #
          \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
          time nice ${dir_imod}/bin/clip unpack ${float} -D "${import_defects}" ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
        else
          #################################################################################
          ${proc_2dx}/linblock "Running clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
          #################################################################################
          #
          ${proc_2dx}/linblock "WARNING: defects list file not found."
          \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
          time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
        endif
      endif
    else
      ${proc_2dx}/linblock "WARNING: defects list file not found."
      #################################################################################
      ${proc_2dx}/linblock "Running clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
      #################################################################################
      #
      \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
      if ( ${import_gainref}x != "x" ) then
        echo Running: time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
        time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        echo Running: time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} FASTDISK/${raw_gaincorrectedstack}.mrcs
        time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} FASTDISK/${raw_gaincorrectedstack}.mrcs
      endif
      echo ":: Done 1"
    endif
    #
    echo "set raw_gaincorrectedstack = ${raw_gaincorrectedstack}"  >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: FASTDISK/${raw_gaincorrectedstack}.mrcs <Gain-corrected stack (stack)>" >> LOGS/${scriptname}.results
  endif
endif
#
echo "<<@progress: 20>>"  
#
if ( ${import_rawstack_type} == "2" ) then  
  if ( ${do_unpack} == "y" ) then
    if ( -e ${raw_gaincorrectedstack}.mrcs ) then
      set SERIALEM_FACTOR = 1.0
      echo "::Raw movie as gain-corrected stack is present as MRCS file."
      cp -f ${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}.mrcs
    else if ( -e ${raw_gaincorrectedstack}.tif ) then
      set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: clip unpack ${import_rawstack} ${raw_gaincorrectedstack}.mrcs"
      #################################################################################
      #
      set float = "-m 2"
      \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
      time nice ${dir_imod}/bin/clip unpack ${float} ${raw_gaincorrectedstack}.tif FASTDISK/${raw_gaincorrectedstack}.mrcs
      #
    else if ( -e ${raw_gaincorrectedstack}.mrc ) then
      set SERIALEM_FACTOR = 1.0
      \mv -f ${raw_gaincorrectedstack}.mrc FASTDISK/${raw_gaincorrectedstack}.mrcs
      echo "::Raw movie as gain-corrected stack is present as MRC file. Now renamed to MRCS."
    else
      #################################################################################
      ${proc_2dx}/protest "${raw_gaincorrectedstack}.mrcs not found, also not at mrc or tif."
      #################################################################################
      #
    endif
    echo "set raw_gaincorrectedstack = ${raw_gaincorrectedstack}"  >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: FASTDISK/${raw_gaincorrectedstack}.mrcs <Gain-corrected stack (stack)>" >> LOGS/${scriptname}.results
  endif
endif
#
if ( ${take_header_orig_sample_pixel} == "y" ) then
# Here we read the header to get the original pixel size, and set sample_pixel for the first time.
  if ( ${do_unpack} == "y" ) then
	if ( ${import_rawstack_type} == "1" || ${import_rawstack_type} == "2" ) then
	  echo "::Gain-corrected stack is ${raw_gaincorrectedstack}.mrcs"
	  #
	  set sample_pixel = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep Scale | cut -d\( -f2 | cut -dx -f1`
	  #
	  set movie_imagenumber_total = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep "Image size"  | cut -d\, -f3 | cut -d\) -f1`
	  echo "set movie_imagenumber_total = ${movie_imagenumber_total}" >> LOGS/${scriptname}.results
	  #
	else
	  #
	  set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
	  set sample_pixel = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep Scale | cut -d\( -f2 | cut -dx -f1`
	  #
	endif
	set orig_sample_pixel = ${sample_pixel}
  else
	if ( ${import_rawstack_type} == "1" || ${import_rawstack_type} == "2" ) then
	  #
	  set sample_pixel = `${dir_imod}/bin/clip info ${import_rawstack} | grep Scale | cut -d\( -f2 | cut -dx -f1`
	  #
	  set movie_imagenumber_total = `${dir_imod}/bin/clip info ${import_rawstack} | grep "Image size"  | cut -d\, -f3 | cut -d\) -f1`
	  echo "set movie_imagenumber_total = ${movie_imagenumber_total}" >> LOGS/${scriptname}.results
	  #
	else
	  #
	  set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
	  set sample_pixel = `${dir_imod}/bin/clip info ${import_rawstack} | grep Scale | cut -d\( -f2 | cut -dx -f1`
	  #
	endif
	set orig_sample_pixel = ${sample_pixel}
  endif
else
# Or, the user provides the orig_sample_pixel and this becomes our initial sample_pixel.
	set sample_pixel = ${orig_sample_pixel}
endif
#
if ( ${Calc_from_sample_pixel} == "y" ) then
  set magnification = `echo "scale=3; ${stepdigitizer} * 10000 / ${sample_pixel}" | bc ` 
  echo "set magnification = ${magnification}"  >> LOGS/${scriptname}.results
endif
#
if ( ${do_unpack} == "n" && ${import_bin} != "0" && ${import_bin} != "1" ) then
  ${proc_2dx}/protest "ERROR: unpacking required if you want to bin before MotionCor2.  Aborting."
endif
if ( ${do_unpack} == "y" && ${import_bin} != "0" && ${import_bin} != "1" ) then
  set width = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f1`
  set height = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f2`
  set widthbig = `echo ${width} 4096 | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s } '`
  if ( ${widthbig} == "1" ) then
    if ( ${import_bin} == "2" ) then
      set xdim = `echo ${width}  | awk '{ s = int($1 / 2) } END { print s }'`
      set ydim = `echo ${height} | awk '{ s = int($1 / 2) } END { print s }'`
      echo ":: Image has size ${width},${height}, which is too large. Fourier-cropping it to ${xdim},${ydim}."
      set sample_pixel = `echo ${sample_pixel} | awk '{ s = 2 * $1 } END { print s }'`
      echo "::SERIALEM_FACTOR is reduced by 4, to account for 2x2 Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} | awk '{ s = $1 / 4.0 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: resample_mp.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      \rm -f dummy.mrc
      \ln -s FASTDISK/${raw_gaincorrectedstack}_large.mrcs dummy.mrc
      time nice +19 ${dir_frealign}/bin/resample_mp.exe << eot
dummy.mrc
FASTDISK/dummyout.mrc
no
no
${xdim}
${ydim}
eot
      if ( -e FASTDISK/dummyout.mrc ) then
        \mv -f FASTDISK/dummyout.mrc FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        ${proc_2dx}/protest "ERROR occured in frealign resample_mp.exe"
      endif
      \rm -f dummy.mrc
      #
    endif
    if ( ${import_bin} == "3" ) then
      #
      set xdim = `echo ${width}  | awk '{ s = int($1 / 2) } END { print s }'`
      set ydim = `echo ${height} | awk '{ s = int($1 / 2) } END { print s }'`
      echo ":: Image has size ${width},${height}, which is too large. Fourier-cropping it to ${xdim},${ydim}."
      set sample_pixel = `echo ${sample_pixel} | awk '{ s = 2 * $1 } END { print s }'`
      echo "::SERIALEM_FACTOR is reduced by 4, to account for 2x2 Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} | awk '{ s = $1 / 4.0 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: newstack -ftreduce 2 -input ${raw_gaincorrectedstack}_large.mrcs -output ${raw_gaincorrectedstack}.mrcs"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      time nice +19 ${dir_imod}/bin/newstack -ftreduce 2 -mode 2 -input FASTDISK/${raw_gaincorrectedstack}_large.mrcs -output FASTDISK/${raw_gaincorrectedstack}.mrcs 
      #
    endif
    if ( ${import_bin} == "4" ) then
      #
      set factor = `echo ${sample_pixel} ${import_bin_target} | awk '{ s = $2 / $1 } END { print s }'`
      set factor2 = `echo ${factor} | awk '{ s = $1 * $1 } END { print s }'`
      set xdim = `echo ${width}  ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ydim = `echo ${height} ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ratio = `echo ${width} ${height} ${xdim} ${ydim} | awk '{ s = ($1 / $3) / ($2 / $4) } END { printf("%16.9f",s) }'`
      echo ":: Image has size ${width},${height} px at ${sample_pixel} A/px, which is too large. Fourier-cropping it to ${xdim},${ydim}, to reach ${import_bin_target} A/px."
      echo ":: Pixel aspect ratio will be 1:${ratio}."
      set sample_pixel = ${import_bin_target}
      echo "::SERIALEM_FACTOR is reduced by ${factor2}, to account for Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} ${factor2} | awk '{ s = $1 / $2 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: resample_mp.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      \rm -f dummy.mrc
      \ln -s FASTDISK/${raw_gaincorrectedstack}_large.mrcs dummy.mrc
      time nice +19 ${dir_frealign}/bin/resample_mp.exe << eot
dummy.mrc
FASTDISK/dummyout.mrc
no
no
${xdim}
${ydim}
eot
      if ( -e FASTDISK/dummyout.mrc ) then
        \mv -f FASTDISK/dummyout.mrc FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        ${proc_2dx}/protest "ERROR occured in frealign resample_mp.exe"
      endif
      \rm -f dummy.mrc
      #
    endif
    if ( ${import_bin} == "5" ) then
      #
      set factor = `echo ${sample_pixel} ${import_bin_target} | awk '{ s = $2 / $1 } END { print s }'`
      set factor2 = `echo ${factor} | awk '{ s = $1 * $1 } END { print s }'`
      set xdim = `echo ${width}  ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ydim = `echo ${height} ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ratio = `echo ${width} ${height} ${xdim} ${ydim} | awk '{ s = ($1 / $3) / ($2 / $4) } END { printf("%16.9f",s) }'`
      echo ":: Image has size ${width},${height} px at ${sample_pixel} A/px, which is too large. Fourier-cropping to reach ${import_bin_target} A/px."
      set sample_pixel = ${import_bin_target}
      echo "::SERIALEM_FACTOR is reduced by ${factor2}, to account for Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} ${factor2} | awk '{ s = $1 / $2 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: fFourierCrop.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      #
      time nice +19 ${bin_2dx}/fFourierCrop.exe << eot
FASTDISK/${raw_gaincorrectedstack}_large.mrcs
FASTDISK/${raw_gaincorrectedstack}.mrcs
y
${import_bin_target}
doc.txt
n
eot
      set imagesidelength = `cat doc.txt | head -n 4 | tail -n 1 | cut -d\# -f1`
      echo "set imagesidelength = ${imagesidelength}" >> LOGS/${scriptname}.results
      #
    endif
  else
    echo ":: Image has size ${width}, which is small enough, no binning done."
  endif
  #
endif
#
echo "<<@progress: 30>>"
#
set frame_image_dose_source = ${movie_image_dose_source}
set frame_image_dose_factor = ${movie_image_dose_factor}
# echo ${movie_image_dose_source} ${frame_image_dose_source}
echo "set sample_pixel = ${sample_pixel}"  >> LOGS/${scriptname}.results
if ( ${do_unpack} == "y" ) then
  if ( -e FASTDISK/${raw_gaincorrectedstack}_large.mrcs ) then
    set param1 = FASTDISK/${raw_gaincorrectedstack}_large.mrcs
    set param2 = ${sample_pixel}
    set param3 = ${SERIALEM_FACTOR}
    source ${proc_2dx}/2dx_calc_framedose_sub.com 
  else
    set param1 = FASTDISK/${raw_gaincorrectedstack}.mrcs
    set param2 = ${sample_pixel}
    set param3 = ${SERIALEM_FACTOR}
    source ${proc_2dx}/2dx_calc_framedose_sub.com 
  endif
else
  set param1 = ${import_rawstack}
  set param2 = ${sample_pixel}
  set param3 = ${SERIALEM_FACTOR}
  source ${proc_2dx}/2dx_calc_framedose_sub.com 
endif
echo "set frame_measured_image_dose = ${frame_measured_image_dose}" >> LOGS/${scriptname}.results
echo "set frame_image_dose = ${frame_image_dose}" >> LOGS/${scriptname}.results
echo "set frame_image_counts = ${frame_image_counts}" >> LOGS/${scriptname}.results
echo "set frame_dose = ${frame_dose}" >> LOGS/${scriptname}.results
echo "set total_dose = ${total_dose}" >> LOGS/${scriptname}.results
#
if ( ${tempkeep} == "n" ) then
  \rm -f FASTDISK/${raw_gaincorrectedstack}_large.mrcs
endif
#
if ( ${do_unpack} == "y" && ${import_produce_gainref2D} == "y" ) then
  #################################################################################
  ${proc_2dx}/linblock "Running: avgstack on ${raw_gaincorrectedstack}.mrcs"
  #################################################################################
  #
  ${dir_imod}/bin/avgstack << eot
FASTDISK/${raw_gaincorrectedstack}.mrcs
${raw_gaincorrectedstack}.mrc
/
eot
  echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}.mrc <Gain-corrected average image (2D)>" >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 50>>"
echo "<<@evaluate>>"
#
if (  ${do_unpack} == "y" && ${crop_histogram} == "y" ) then
  set val_avg = `${dir_imod}/bin/clip stats ${raw_gaincorrectedstack}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $1}'`
  set val_stddev = `${dir_imod}/bin/clip stats ${raw_gaincorrectedstack}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $2}'`
  set newmin = `echo "scale=6; ${val_avg} - ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  set newmax = `echo "scale=6; ${val_avg} + ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  echo ":Statistics on 2D images are:  Average = ${val_avg}, StdDev = ${val_stddev}"
  #################################################################################
  ${proc_2dx}/linblock "Truncating histogram of 2D average to ${newmin} ... ${newmax}"
  #################################################################################
  #
  time nice +19 ${dir_imod}/bin/clip truncate -l ${newmin} -h ${newmax} ${raw_gaincorrectedstack}.mrc ${raw_gaincorrectedstack}_trunc.mrc
  \mv -f ${raw_gaincorrectedstack}_trunc.mrc ${raw_gaincorrectedstack}.mrc
endif
#
if ( ${do_unpack} == "y" ) then
  \rm -f tmp.png
  \rm -f tmp2.png
  \rm -f STATUS/1-image.jpg
  ${app_2dx_mrc_converter} --size 400 ${raw_gaincorrectedstack}.mrc tmp.png 
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${raw_gaincorrectedstack}.mrc.png 10 365 0 "${date_text}"
  if ( ${blurkernel} != "0" ) then
    ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 135 180 ${blurkernel} "I N T E N T I O N A L L Y"
    ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png tmp.png 135 195 0             "   B  L  U  R  R  E  D"
  endif
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-image.jpg 10 365 0 "${date_text}"
  \rm -f tmp.png 
  \rm -f tmp2.png
endif
#
if ( ${do_unpack} == "y" ) then
  ##########################################################################
  ${proc_2dx}/linblock "Copying raw mrcs file..."
  ##########################################################################
  cp FASTDISK/${raw_gaincorrectedstack}.mrcs ./
endif
#
if (  ${do_unpack} == "y" && ${import_produce_gainref2D} == "y" && ${import_produce_gainref2Dfft} == "y" ) then
  #################################################################################
  ${proc_2dx}/linblock "Running: labelh.exe to normalize MRC image"
  #################################################################################
  # 
  echo "<<@progress: 45>>"
  #
  \rm -f FASTDISK/${raw_gaincorrectedstack}_norm.mrc
  #
  time ${bin_2dx}/labelh.exe << eot
${raw_gaincorrectedstack}.mrc
42
FASTDISK/${raw_gaincorrectedstack}_norm.mrc
eot
  ${proc_2dx}/linblock "Running: clip fft FASTDISK/${raw_gaincorrectedstack}_norm.mrc ${raw_gaincorrectedstack}_fft.mrc"
  time nice +19 ${dir_imod}/bin/clip fft FASTDISK/${raw_gaincorrectedstack}_norm.mrc ${raw_gaincorrectedstack}_fft.mrc
  echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}_fft.mrc <Gain-corrected average image FFT (2D)>" >> LOGS/${scriptname}.results
  \rm -f FASTDISK/${raw_gaincorrectedstack}_norm.mrc
  \rm -f tmp.png
  \rm -f tmp2.png
  \rm -f STATUS/1-FFT.jpg
  ${app_2dx_mrc_converter} --size 400 ${raw_gaincorrectedstack}_fft.mrc tmp.png
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "FFT of raw average"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${raw_gaincorrectedstack}_fft.mrc.png 10 365 0 "${date_text}"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-FFT.jpg 10 365 0 "${date_text}"
  \rm -f tmp.png
  \rm -f tmp2.png
endif
#
echo "set SERIALEM_FACTOR = ${SERIALEM_FACTOR}"  >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
exit
#
